let baseUrl = '/api' //线上
let wsUrl = 'ws://localhost:8080'
function deleteEmptyObject(object) {
	for (var i in object) {
		var value = object[i]
		const type = typeof value
		// 去除空格1
		if (type === 'string') {
			object[i] = trim(value)
		} 

		if (typeof value === 'object') {
			// 是否引用数据类型
			if (Array.isArray(value)) {
				if (value.length === 0) {
					delete object[i]
					continue
				}
			}
			// 删除空格
			deleteEmptyObject(value)
			if (isEmpty(value)) {
				delete object[i]
			}
		} else {
			// 基本数据类型
			if (value === '' || value === null || value === undefined) {
				delete object[i]
			}
		}
	}
	return object
}

function trim(str) { // 删除左右两端的空格
	return str.replace(/(^\s*)|(\s*$)/g, '')
}

function isEmpty(object) {
	for (var name in object) {
		return false
	}
	return true
}
const https = (
url = '', 
data = {
	
}, 
header = {
	Authorization: 'Bearer '+uni.getStorageSync('token')
},
method='') => {
	return new Promise((resolve, reject) => {
		uni.showLoading({
			title:'加载中...'
		})
		uni.request({
			method: method||'post',
			url: baseUrl + url,
			data: deleteEmptyObject(data),
			header: header,
			dataType: 'json',
		}).then((response) => {
			uni.hideLoading();
			let res = response.data;
			const msg = res.msg
			if (res.code === 401) {
				uni.showToast({
					title: msg,
					icon: "error"
				})
				setTimeout(function() {
					uni.clearStorageSync()
					uni.reLaunch({
						url:'/pages/login/login'
					})
				}, 3000);
			}else if(res.code == 500 || res.code == 1) {
				console.log(res.status,'0000')
				uni.showToast({
					title: msg,
					icon: "none"
				})
			}else if(res.code == 200){
				
				resolve(res);
			}
		}).catch(error => {
			let [err, res] = error;
			reject(err)
		})
	});
}
const sortFunction = (a, b) => {
	// 获取原字母与转小写字母后的2倍ascii码
	let a1 = 2 * a.charCodeAt(),
		b1 = 2 * b.charCodeAt(),
		a2 = 2 * a.toLowerCase().charCodeAt(),
		b2 = 2 * b.toLowerCase().charCodeAt();
		// 如果原字母大写则结果+1
		a2 = a1 == a2 ? a2 : a2 + 1;
		b2 = b1 == b2 ? b2 : b2 + 1;
		return a2 - b2;
}
export {
	baseUrl,
	wsUrl,
	https,
	sortFunction
}
